package test;
import org.apache.lucene.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.*;

@SuppressWarnings({ "deprecation", "unused" })
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 Analyzer analyzer = new StandardAnalyzer();
		 	try{
		 		
		    // Store the index in memory:
		    Directory directory = new RAMDirectory();
		    IndexWriter iwriter;
		    // To store an index on disk, use this instead:
		    //Directory directory = FSDirectory.getDirectory("/tmp/testindex");
		    IndexWriter IndexWriter = new IndexWriter(directory, analyzer, true);
		    iwriter = IndexWriter;
		    iwriter.setMaxFieldLength(25000);
		    Document doc = new Document();
		    
		    String text = "This is the text to be indexed.";
		    String text2 = "Este es otro text";
		    String text3 = "Este es otro pero no";
		    String text4 = "Este otro que tiene text";
		    
		    doc.add(new Field("fieldname", text, Field.Store.YES,Field.Index.TOKENIZED));
		    iwriter.addDocument(doc);
		    
		    Document doc2 = new Document();
		    doc2.add(new Field("fieldname", text2, Field.Store.YES,Field.Index.TOKENIZED));
		    iwriter.addDocument(doc2);
		    
		    Document doc3 = new Document();
		    doc3.add(new Field("fieldname", text3, Field.Store.YES,Field.Index.TOKENIZED));
		    iwriter.addDocument(doc3);
		    
		    Document doc4 = new Document();
		    doc4.add(new Field("fieldname", text4, Field.Store.YES,Field.Index.TOKENIZED));
		    iwriter.addDocument(doc4);
		    
		    iwriter.optimize();
		    iwriter.close();
		    
		    // Now search the index:
		    IndexSearcher isearcher = new IndexSearcher(directory);
		    // Parse a simple query that searches for "text":
		    QueryParser parser = new QueryParser("fieldname", analyzer);
		    Query query = parser.parse("text");
		    Hits hits = isearcher.search(query);

		    // Iterate through the results:
		    for (int i = 0; i < hits.length(); i++) {
		      Document hitDoc = hits.doc(i);
		       System.out.print(hitDoc.get("fieldname")+"/n");
		    }
		    
		    isearcher.close();
		    directory.close();}
		 	catch(Exception e){}
		    
	}

}
